import api from "../../api/index.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['待领取', '已领取', '失效|已用', '口令'],
    activeIndex:0,
    showPwdPop: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    getApp().initLanguage(this)
    wx.setNavigationBarTitle({
      title: "优惠券标题",
    })
    if (e.activeIndex) {
      this.data.activeIndex= e.activeIndex
    }else{
      this.data.activeIndex= 0
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    this.setData({
      activeIndex: parseInt(this.data.activeIndex)
    });
    this._onShow(this.data.activeIndex)
  },
  _onShow: function () {
    let couponStatus = 'A'
    if (this.data.activeIndex == 1) {
      couponStatus = 'B'
    }else if (this.data.activeIndex == 2) {
      couponStatus = 'CD'
    }
    this.getCouponList(couponStatus)
  },

  tabClick: function (e) {
    this.setData({
      activeIndex: e.detail.index
    });
    let couponStatus;
    if (this.data.activeIndex == 0) {
      couponStatus = "A"
    }
    if (this.data.activeIndex == 1) {
      couponStatus = "B"
    }
    if (this.data.activeIndex == 2) {
      couponStatus = "CD"
    }
    if (this.data.activeIndex == 3) {
      return
    }
    this.getCouponList(couponStatus)
  },
  getCouponList(couponStatus){
    api.couponList({
      data: {
        couponStatus
      },
    }).then((res) => {
      this.setData({
        coupons: res.data
      })
    })
  },
  getCounpon(e) {
    // if (!this.data.couponPwd) {
    //   wx.showToast({
    //     title: this.data.$t.coupons.inputpassword,
    //     icon: 'none'
    //   })
    //   return
    // }
    let that = this;
    api.couponReceive({
      method: "POST",
      data: {
        couponId: e.currentTarget.dataset.id
      },
    }).then((res) => {
      wx.showToast({
        title: "领券成功",
        icon: 'success',
        success:function(){
          //切换已领tab
          that.tabClick({detail:{index:1}})
        }
      })
    })
  },
  async touse(e) {
    wx.switchTab({
      url: "/pages/order/index"
    })
  },
  pwdCouponChange(e) {
    this.setData({
      couponPwd: e.detail.value
    })
  },
  closePwd() {
    this.setData({
      showPwdPop: false
    })
  },
  async exchangeCoupons() {
    if (!this.data.number) {
      wx.showToast({
        title: "请输入券号",
        icon: 'none'
      })
      return
    }
    if (!this.data.pwd) {
      wx.showToast({
        title: "请输入密码",
        icon: 'none'
      })
      return
    }
    this.setData({
      exchangeCouponsLoading: true
    })
  },
  onShareAppMessage: function () {
    return {
      title: wx.getStorageSync('mallName') + this.data.$t.coupons.Invitingcoupons,
      path: '/pages/coupons/index?inviter_id=' + wx.getStorageSync('uid')
    }
  },
  onShareTimeline() {
    return {
      title: wx.getStorageSync('mallName') + this.data.$t.coupons.Invitingcoupons,
      query: 'inviter_id=' + wx.getStorageSync('uid'),
      imageUrl: wx.getStorageSync('share_pic')
    }
  },
})